package org.mm.frame.format;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.Formatter;
import org.springframework.format.number.NumberFormatter;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;

public class ApplicationConversionService extends FormattingConversionServiceFactoryBean {

	final Logger log = LoggerFactory.getLogger(ApplicationConversionService.class);
		
	private Set<Formatter<?>> formatters = new HashSet<Formatter<?>>();
	
	private String datePattern = "yyyy-MM-dd";	
	
	private String numberPattern = "#,##0.00";
	
	
	public String getDatePattern() {
		return datePattern;
	}

	@Autowired(required=false)
	public void setDatePattern(String datePattern) {
		this.datePattern = datePattern;
	}
		
	/**
	 * setNumberPattern
	 * @param numberPattern
	 */
	public void setNumberPattern(String numberPattern) {
		this.numberPattern = numberPattern;
	}

	@PostConstruct
	public void init() {
		formatters.add(getDateTimeFormatter());
		formatters.add(getNumberFormatter());
		setFormatters(formatters);		
	}
	

	public Formatter<DateTime> getDateTimeFormatter() {
		return new PersonalDateTimeFormatter(DateTimeFormat.forPattern(datePattern));
	}
	
	public Formatter<Number> getNumberFormatter() {
		return new NumberFormatter(numberPattern);
	}
}

